﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StaffingSystem.EF;
using System.Text;
using StaffingSystem.Business;
using StaffingSystem.Tool;
using System.Data.SqlClient;
using StaffingSystem.Models;

namespace StaffingSystem.Controllers
{
    public class MessageController : Controller
    {
        //调用EF
        Management management = new Management();
        public JsonResult result;

        // GET: Message

        public ActionResult Basics()
        {
            return View();
        }

        //主页面 
        [HttpPost]
        public ActionResult Basics(PageViewModel page )
        {
            string where = "";
            if (page.Where != null)
            {
                where += page.Where;
            }
            else
            {
                where += string.Empty;
            }
            ProcHepler<personnelinfor> userlist = new ProcHepler<personnelinfor>();

            PageModel<personnelinfor> pagemodel = new PageModel<personnelinfor>();
            pagemodel.PageSize = page.PageSize;
            pagemodel.PageNumber = page.PageNumber;
            pagemodel.AscOrDesc = string.Empty;
            pagemodel.OrderID = "Id";
            pagemodel.PageKey = "Id";
            pagemodel.TableName = "personnelinfor";
            pagemodel.Where =where;
            
            pagemodel = userlist.GetPageList(pagemodel);

            string data = JsonTool.JsonSerializer(pagemodel);

            result = DataHelper.GetResult(ExecuteState.Succeed, "获取列表成功！", data);
            return result;
        }




        //编辑
        public ActionResult Compile(int id)
        {
            personnelinfor list = management.personnelinfor.FirstOrDefault(m => m.Id == id);
            return View("Compile", list);
        }

        [HttpPost]
        public ActionResult Compile(personnelinfor person)
        {
            try
            {
                EmployeeCompile employee = new EmployeeCompile();
                int succeed = employee.UpdateCompile(person);
                if (succeed > 0)
                {
                    result = DataHelper.GetResult(ExecuteState.Succeed, "信息修改成功 ！", "");
                }
                else
                {
                    result = DataHelper.GetResult(ExecuteState.Failed, "信息修改失败 ！", "");
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        //删除
        [HttpPost]
        public ActionResult Delete(int id)
        {

            Management person = new Management();
            personnelinfor td = person.personnelinfor.FirstOrDefault(m => m.Id == id);
            person.personnelinfor.Remove(td);
            person.SaveChanges();
            JsonResult result = new JsonResult();
            result.Data = new
            {
                State = 0,
                Message = "删除成功"
            };
            return result;

        }


    }
}

